package cn.sxt.clinic.controller;

import cn.sxt.clinic.pojo.Job;
import cn.sxt.clinic.pojo.ResultCode;
import cn.sxt.clinic.service.IJobService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/job")
public class JobController {

    @Autowired
    private IJobService jobService;

    @RequiresPermissions("job:jobManager")
    @RequestMapping("/jobManager")
    public String jobManager(){
        return "jobManager";
    }

    @RequiresPermissions("job:list")
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   String keyword){
        IPage<Job> page=new Page<>(pageNum,pageSize);
        QueryWrapper<Job> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)){
            wrapper.like("permission_name",keyword);
        }
        IPage<Job> result = jobService.page(page, wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",result.getTotal());
        map.put("rows",result.getRecords());
        return map;
    }

    @RequestMapping("/jobEdit")
    public String jobEdit(Integer jobId, Model m){
        if (jobId != null){
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("job_id",jobId);
            Job job = jobService.getOne(queryWrapper);
            m.addAttribute("job",job);
        }

        return "jobEdit";
    }

    @RequiresPermissions("job:insert")
    @RequestMapping("/insert")
    @ResponseBody
    public ResultCode insertJob(Job job){

        boolean update = jobService.saveOrUpdate(job);

        ResultCode resultCode =null;
        if (update){
            resultCode = new ResultCode(1, "添加成功");
        }else{
            resultCode = new ResultCode(0,"添加失败");
        }
        return resultCode;
    }

    @RequiresPermissions("job:update")
    @RequestMapping("/update")
    @ResponseBody
    public ResultCode updateJob(Job job){

        boolean update = jobService.saveOrUpdate(job);

        ResultCode resultCode =null;
        if (update){
            resultCode = new ResultCode(1, "更新成功");
        }else{
            resultCode = new ResultCode(0,"更新失败");
        }
        return resultCode;
    }

    @RequiresPermissions("job:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public ResultCode delete(Integer jobId){
        boolean b = jobService.removeById(jobId);
        ResultCode resultCode =null;
        if (b){
            resultCode = new ResultCode(1, "删除成功");
        }else{
            resultCode = new ResultCode(0,"删除失败");
        }
        return resultCode;
    }

}

